var utils = require('../../../utils/util.js')
var c = require('../../../utils/config.js')
//获取应用实例
const app = getApp()

Page({
    data: {
        userinfo:wx.getStorageSync("userinfo"),
        imgurl: [],
        img_url: c.img_url,
        ad_img:[],
        goods:[],
        keyword:"",
        share_point: 0,
        share_start_content: "",
        share_end_content: ""
    },
    //事件处理函数
    onLoad: function (options) {
        utils.updateUserinfo();
        this.setData({
            userinfo:wx.getStorageSync("userinfo")
        });
        if (options.invitecode){
          wx.setStorageSync('invitecode', options.invitecode);
        }
        this.getSlide();
        this.getGoods();
        this.getShareInfo();
        this.getAdSlide();
    },
    //下拉刷新
    onPullDownRefresh: function () {
        utils.updateUserinfo();
        this.setData({
            userinfo:wx.getStorageSync("userinfo")
        });
        wx.stopPullDownRefresh();
    },
    /**
     * 幻灯
     */
    getSlide: function () {
        var that = this;
        var data = {
            url: "/zfcom/slide/lists",
            cat_id: 1,
            page: 1,
            limit: 4,
            keyword: ""
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    data[index].pic = c.img_url + data[index].pic;
                }
                that.setData({
                    imgurl: data
                });
            } else {

            }
        });
    },
    /**
     * 广告图
     */
    getAdSlide: function () {
      var that = this;
      var data = {
        url: "/zfcom/slide/lists",
        cat_id: 17,
        page: 1,
        limit: 1,
        keyword: ""
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          var data = res.data;
          for (var index in data) {
            data[index].pic = c.img_url + data[index].pic;
          }
          that.setData({
            ad_img: data
          });
        } else {

        }
      });
    },
    /**
     * 商品推荐
     */
    getGoods: function () {
        var that = this;
        var data = {
            url: "/wsgoods/goods/lists",
            keyword:"",
            cat_id: "",
            rec_id: 1,
            page: 1,
            limit: 100,
            order:1
        };
        utils.request(data).then(function (res) {
            if (res.code == 0) {
                var data = res.data;
                for (var index in data) {
                    var points_price = data[index]['price']-data[index]['point'];
                    data[index]['points_price'] = points_price.toFixed(2);
                }
                that.setData({
                    goods: data
                });
            } else {

            }
        });
    },
    /**
     * 搜索内容
     */
    editKeyword: function (e) {
        this.setData({
            keyword: e.detail.value
        })
    },
    /**
     * 搜索商品
     */
    searchGoods: function () {
        var keyword = this.data.keyword;
        wx.navigateTo({
            url: '/pages/shop/goodslist/index?keyword=' + keyword,
        })
    },

    tocart:function(){
        wx.switchTab({
            url: '/pages/shop/ordercart/index'
        })
    },
    more:function(){
        wx.switchTab({
            url: '/pages/shop/shop/index',
        })
    },
    test:function(){
        utils.errMsg("暂未开放");
    },
    //分享
    onShareAppMessage: function (res) {
      var that = this;
      if (res.from === "button") {
        var id = res.target.dataset.id;
        var name = res.target.dataset.name;
        var pic = res.target.dataset.pic;
        var description = res.target.dataset.description;
        var price = res.target.dataset.price;
        var point = res.target.dataset.point;
        var points_price = price-point;
        return {
          title: that.data.share_start_content + name + " ￥" + points_price.toFixed(2) + that.data.share_end_content,
          path: '/pages/shop/goodsdetail/index?id=' + id + "&invitecode=" + wx.getStorageSync("userinfo").invitecode,
          imageUrl: pic,
          success: function (res) {
            wx.getSystemInfo({
              success: function (d) {
                var token = wx.getStorageSync("userinfo").wap_token;
                var data = {
                  url: "/wsgoods/usershare/add",
                  token: token,
                  type: 0,
                  id: id
                };
                utils.request(data).then(function (res) {
                  if (res.code == 0) {

                  }
                });
              }
            })
          },
        }
      }
    },
    jump: function (res) {
      utils.r("/pages/shop/goodsdetail/index?id=" + res.currentTarget.dataset.id);
    },
    //分享信息
    getShareInfo: function () {
      var that = this;
      var data = {
        url: "/zfcom/com/deploy",
        uuid: "share_point,share_start_content,share_end_content",
      };
      utils.request(data).then(function (res) {
        if (res.code == 0) {
          that.setData({
            share_point: res.data.share_point,
            share_start_content: res.data.share_start_content,
            share_end_content: res.data.share_end_content,
          });
        }
      });
    },
})
